using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using DDay.iCal;
using DDay.iCal.Components;
using DDay.iCal.Serialization;
using DDay.iCal.DataTypes;

namespace quickTask
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            initCalList();
            initCategoryList();
        }


        public void saveCalList()
        {
            saveList(mCalList, CALENDARS_FILE_NAME);
        }

        public void saveCategoryList()
        {
            saveList(mCategoryList, CATEGORIES_FILE_NAME);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            initCalendarComboBox();
            initCategoriesComboBox();
        }


        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            followupDragEnter(e);
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            summaryTextBox.Text = e.Data.GetData(DataFormats.Text).ToString();
        }

        #region contextMenuStripEventHandlers

        private void calendarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CalendarDialog theCalDialog = new CalendarDialog(ref mCalList);
            theCalDialog.ShowDialog(this);

            initCalList();
            initCalendarComboBox();
        }

        private void categoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CategoryDialog theCategoryDialog = new CategoryDialog(mCategoryList);
            theCategoryDialog.ShowDialog(this);

            initCategoryList();
            initCategoriesComboBox();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutForm theAboutForm = new AboutForm();
            theAboutForm.ShowDialog(this);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion //NOTE : end contextMenuStripEventHandlers

        private void calendarComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            mCalFile = mCalList[calendarComboBox.SelectedIndex];
            reloadCalendar();
        }

        private void summaryTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char) Keys.Enter)
            {
                addButton_Click(this, EventArgs.Empty);                
            }
        }

        private void summaryTextBox_Enter(object sender, EventArgs e)
        {
            if(calendarComboBox.SelectedItem == null)
            {
                showInfoNoCalendar();
                return;
            }

            reloadCalendar();
        }

        private void descriptionTextBox_DragEnter(object sender, DragEventArgs e)
        {
            followupDragEnter(e);
        }

        private void descriptionTextBox_DragDrop(object sender, DragEventArgs e)
        {
            descriptionTextBox.Text = e.Data.GetData(DataFormats.Text).ToString();               
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            if (calendarComboBox.SelectedItem == null)
            {
                showInfoNoCalendar();
                return;
            }

            reloadCalendar();
            mCalWorker.addTodo(summaryTextBox.Text, descriptionTextBox.Text,
                categoryComboBox.Text);
            mCalWorker.save(mCalFile);

            summaryTextBox.Text = "";
            descriptionTextBox.Text = "";

            updateTodosCount();
        }

        private void showInfoNoCalendar()
        {
            MessageBox.Show("No calendar selected. Please select a calendar first.",
            "No Calendar", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void initCalendarComboBox()
        {
            calendarComboBox.Items.Clear();
            foreach(string str in mCalList)
            {
                calendarComboBox.Items.Add(Path.GetFileName(str));
            }
        }

        private void initCategoriesComboBox()
        {
            categoryComboBox.Items.Clear();

            //NOTE : add blank category 
            categoryComboBox.Items.Add("");

            foreach(string str in mCategoryList)
            {
                categoryComboBox.Items.Add(str);
            }
        }

        private void initCalList()
        {
            initList(mCalList, CALENDARS_FILE_NAME);
        }

        private void initCategoryList()
        {
            initList(mCategoryList, CATEGORIES_FILE_NAME);
        }

        private void initList(List<string> theList, string fileName)
        {
            theList.Clear();
            using (StreamReader sr = File.OpenText(generateConfigPath(fileName)))
            {
                string readString;
                while ((readString = sr.ReadLine()) != null)
                {
                    if(readString != "") theList.Add(readString);
                }
            }
        }

        private void saveList(List<string> theList, string fileName)
        {
            using (StreamWriter sw = File.CreateText(generateConfigPath(fileName)))
            {
                foreach (string str in theList)
                {
                    sw.WriteLine(str);
                }
            }            
        }

        private string generateConfigPath(string configFile)
        {
            //TEMP
            /* TEMP : not compatible with mono
            return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
                "\\QuickTask\\" + configFile;
             * */

            return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
                "/QuickTask/" + configFile;
        }

        private void reloadCalendar()
        {

            mCalWorker.load(mCalFile);
            updateTodosCount();
        }

        private void updateTodosCount()
        {
            todoCountLabel.Text = mCalWorker.CountTodosAll.ToString();
        }

        private const string CALENDARS_FILE_NAME = "calendars.txt";
        private const string CATEGORIES_FILE_NAME = "categories.txt";


        private CalendarWorker mCalWorker = new CalendarWorker();
        private List<string> mCalList = new List<string>();
        private List<string> mCategoryList = new List<string>();
        private string mCalFile = "";



        private void followupDragEnter( DragEventArgs e )
        {
            if(e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }


    }
}